home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / buzzmachines_massive.exe / Dev / Overloader - Au Recorder / nextsunrec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-03  |  10.2 KB  |  355 lines

  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #include "resource.h"
  6.   
  7. double const PI = 3.14159265358979323846;
  8.  
  9. HINSTANCE dllInstance;
  10.  
  11. #define MAX_BUFFER_LENGTH        256            // in number of samples
  12.  
  13. class COOERecorder {
  14. public:
  15.     virtual bool __cdecl Init();
  16.  
  17.     virtual bool __cdecl IsMultiTrack();
  18.     virtual bool __cdecl IsTagged();
  19.     virtual bool __cdecl IsStreamed();
  20.     virtual bool __cdecl IsRequiresWaveout();
  21.     virtual bool __cdecl IsLossyCompression();
  22.  
  23.     virtual bool __cdecl IsSampleRateChangeable();
  24.     virtual bool __cdecl SampleRateChanged(int samplerate);
  25.     virtual bool __cdecl IsBitRateChangeable();
  26.     virtual bool __cdecl BitRateChanged(int bitrate);
  27.  
  28.     virtual bool __cdecl SupportsSampleRate(int samplerate);
  29.     virtual bool __cdecl SupportsBitRate(int bitrate);
  30.  
  31.     virtual bool __cdecl SaveAs(HWND parentwindow);
  32.     virtual bool __cdecl ReadyToRec();
  33.  
  34.     virtual bool __cdecl TrackNames(int track_id, char *track_name);
  35.     virtual bool __cdecl SongTagData(int tag_index, char *tagdata);
  36.  
  37.     virtual bool __cdecl Start(char * filename, int samplespersec);
  38.     virtual bool __cdecl WorkOutput(float *psamples, int numsamples);
  39.     virtual bool __cdecl WorkOutputMulti(int track_id, float *psamples, int numsamples);
  40.  
  41.     virtual bool __cdecl Finish();
  42.     virtual void __cdecl ConfigDlg(HWND parentwindow);
  43.     virtual void __cdecl DispatchCommand(int command_id, int param1, int param2, int param3, int param4);
  44.     virtual void __cdecl DispatchCommandEx(char * str_command, char * str_value);
  45.  
  46.     virtual void __cdecl LoadSettings(char * settingsname, char *username, char *domain);
  47.     virtual void __cdecl SaveSettings(char * settingsname, char *username, char *domain);
  48.  
  49.     virtual char * __cdecl OutputFilename();
  50.     virtual char * __cdecl OutputSize();
  51.     virtual char * __cdecl ExtraInfo(int extra_info_id);
  52.     virtual char * __cdecl RecordersWebSiteURL();
  53.  
  54.     virtual int __cdecl RecorderVersion();
  55.  
  56.     virtual void GetRecorderExtensionsClass(int param, void **exmodule);
  57. };
  58.  
  59. class rec : public COOERecorder {
  60. public:
  61.     virtual bool __cdecl Init();
  62.  
  63.     virtual bool __cdecl IsMultiTrack() { return false; };
  64.     virtual bool __cdecl IsTagged() { return true; };
  65.     virtual bool __cdecl IsStreamed() { return false; };
  66.     virtual bool __cdecl IsRequiresWaveout() { return false; };
  67.     virtual bool __cdecl IsLossyCompression() { return false; };
  68.  
  69.     virtual bool __cdecl IsSampleRateChangeable() { return false; };
  70.     virtual bool __cdecl SampleRateChanged(int samplerate) { return false; };
  71.     virtual bool __cdecl IsBitRateChangeable() { return false; };
  72.     virtual bool __cdecl BitRateChanged(int bitrate) { return false; };
  73.  
  74.     virtual bool __cdecl SupportsSampleRate(int samplerate) { return true; };
  75.     virtual bool __cdecl SupportsBitRate(int bitrate) { return true; };
  76.  
  77.     virtual bool __cdecl SaveAs(HWND parentwindow);
  78.     virtual bool __cdecl ReadyToRec();
  79.  
  80.     virtual bool __cdecl TrackNames(int track_id, char *track_name) { return false; };
  81.     virtual bool __cdecl SongTagData(int tag_index, char *tagdata);
  82.  
  83.     virtual bool __cdecl Start(char * filename, int samplespersec);
  84.     virtual bool __cdecl WorkOutput(float *psamples, int numsamples);
  85.     virtual bool __cdecl WorkOutputMulti(int track_id, float *psamples, int numsamples) { return false; };
  86.  
  87.     virtual bool __cdecl Finish();
  88.     virtual void __cdecl ConfigDlg(HWND parentwindow);
  89.     virtual void __cdecl DispatchCommand(int command_id, int param1, int param2, int param3, int param4);
  90.     virtual void __cdecl DispatchCommandEx(char * str_command, char * str_value);
  91.  
  92.     virtual void __cdecl LoadSettings(char * settingsname, char *username, char *domain) { };
  93.     virtual void __cdecl SaveSettings(char * settingsname, char *username, char *domain) { };
  94.  
  95.     virtual char * __cdecl OutputFilename();
  96.     virtual char * __cdecl OutputSize();
  97.     virtual char * __cdecl ExtraInfo(int extra_info_id) { return "No Extra Info"; };
  98.     virtual char * __cdecl RecordersWebSiteURL() { return "http://www.buzzscene.ca/"; };
  99.  
  100.     virtual int __cdecl RecorderVersion() { return 100; };
  101.  
  102.     virtual void GetRecorderExtensionsClass(int param, void **exmodule) { };
  103. public:
  104.     char myfilename[255];
  105.     FILE * myfilehandle;
  106.     char songtitle[100];
  107.     char songartist[100];
  108.     int writtensofar;
  109.     int bitdepth;
  110.     int samplerate;
  111. };
  112.  
  113. bool rec::Init(){
  114.     sprintf(myfilename, "");
  115.     myfilehandle = NULL;
  116.     writtensofar = 0;
  117.     bitdepth = 0;
  118.     sprintf(songtitle, "");
  119.     sprintf(songartist, "");
  120.     samplerate = 44100;
  121.     return true;
  122. }
  123. bool rec::SaveAs(HWND parentwindow){
  124.     OPENFILENAME ofl;
  125.     char filename[255];
  126.     int nSuccess;
  127.  
  128.     sprintf(filename,"untitled.au");
  129.     ofl.lStructSize = sizeof(ofl);
  130.     ofl.hwndOwner = parentwindow;
  131.     ofl.hInstance = dllInstance;
  132.     ofl.lpstrFilter = "Next\\Sun sound format (*.au)\0*.au\0";
  133.     ofl.lpstrCustomFilter = NULL;
  134.     ofl.nMaxCustFilter = NULL;
  135.     ofl.nFilterIndex = 1;
  136.     ofl.lpstrFile = filename;
  137.     ofl.nMaxFile = 255;
  138.     ofl.lpstrFileTitle = NULL;
  139.     ofl.nMaxFileTitle = NULL;
  140.     ofl.lpstrInitialDir = NULL;
  141.     ofl.lpstrTitle = "Save Song Output as";
  142.     ofl.Flags = OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
  143.     ofl.nFileOffset = 0;
  144.     ofl.nFileExtension = 0;
  145.     ofl.lpstrDefExt = ".au";
  146.     ofl.lCustData = NULL;
  147.     ofl.lpfnHook = NULL;
  148.     ofl.lpTemplateName = NULL;
  149.     nSuccess = GetSaveFileName(&ofl);
  150.     if (nSuccess == 0) {
  151.         sprintf(myfilename, "");
  152.     } else {
  153.         sprintf(myfilename, ofl.lpstrFile);
  154.     }
  155.     return true;
  156. }
  157. char *rec::OutputFilename() {
  158.     return myfilename;
  159. }
  160.  
  161. static union {
  162.     long  longvalue;
  163.     char  longvaluebytes[4];
  164. };
  165.  
  166. static union {
  167.     short thepsamp;
  168.     char  thepsampbytes[2];
  169. };
  170.  
  171. bool rec::SongTagData(int tag_index, char *tagdata) {
  172.     switch (tag_index) {
  173.     case 0: sprintf(songtitle, tagdata); return true; 
  174.     case 1: sprintf(songartist, tagdata); return true;
  175.     case 2: return false;
  176.     default:
  177.         return false;
  178.     }
  179. }
  180.  
  181. bool rec::Start (char * filename, int samplespersec) {
  182.     char addinfo[204];
  183.     short data1 = 0x0000;
  184.     char data1a = 0x00;
  185.     char data1b = 0x00; // custom
  186.     long data2 = 0xFFFFFFFF;
  187.     long data3 = 0x03000000;
  188.     //short data4 = 0x0000;
  189.     long data5 = 0x02000000;
  190.     long datablk=0x00000000;
  191.  
  192.     char longvaluebyte;
  193.     samplerate = samplespersec;
  194.     unsigned int invsamplerate = 44100;
  195.  
  196.     longvalue = samplerate;
  197.     longvaluebyte = longvaluebytes[0];
  198.     longvaluebytes[0] = longvaluebytes[3];
  199.     longvaluebytes[3] = longvaluebyte;
  200.     longvaluebyte = longvaluebytes[1];
  201.     longvaluebytes[1] = longvaluebytes[2];
  202.     longvaluebytes[2] = longvaluebyte;
  203.     invsamplerate = longvalue;
  204.  
  205.  
  206.     myfilehandle = fopen(filename, "wb");
  207.     writtensofar = 0;
  208.  
  209.     if (strlen(songartist) == 0) {
  210.         if (strlen(songtitle) == 0) {
  211.             sprintf(addinfo, "");
  212.         } else {
  213.             sprintf(addinfo, "%s", songtitle);
  214.         }
  215.     } else {
  216.         if (strlen(songtitle) == 0) {
  217.             sprintf(addinfo, "%s", songartist);
  218.         } else {
  219.             sprintf(addinfo, "%s - %s", songartist, songtitle);
  220.         }
  221.     }
  222.     
  223.     fwrite(".snd", 1, 4,myfilehandle);
  224.     
  225.     data1b = 24 + strlen(addinfo)+1;
  226.     fwrite(&data1, sizeof(short), 1, myfilehandle);
  227.     fwrite(&data1a, sizeof(char), 1, myfilehandle);
  228.     fwrite(&data1b, sizeof(char), 1, myfilehandle);
  229.  
  230.     fwrite(&data2, sizeof(long), 1,myfilehandle);
  231.     fwrite(&data3, sizeof(long), 1,myfilehandle);
  232.     fwrite(&invsamplerate, sizeof(long), 1,myfilehandle);
  233. //    fwrite(&data4, sizeof(short), 1,myfilehandle);
  234.     fwrite(&data5, sizeof(long), 1,myfilehandle);
  235.  
  236.     fwrite(addinfo, sizeof(char), strlen(addinfo)+1, myfilehandle);
  237.  
  238.     return true;
  239. }
  240. bool rec::ReadyToRec() {
  241.     if (strcmp(myfilename, "") == 0) {
  242.         return false;
  243.     } else {
  244.         return true;
  245.     }
  246.  
  247. }
  248.  
  249. bool rec::WorkOutput (float *psamples, int numsamples) {
  250.     short psampsm[1024];
  251.     char bufpsampbyte;
  252.     for (int i = 0; i < (numsamples * 2); i++) {
  253.         if (psamples[i] > 32767.0f) psamples[i] = 32767.0f;
  254.         if (psamples[i] < -32766.0f) psamples[i] = -32766.0f;
  255.         psampsm[i] = (short)psamples[i];
  256.         thepsamp = psampsm[i];
  257.         bufpsampbyte = thepsampbytes[0];
  258.         thepsampbytes[0] = thepsampbytes[1];
  259.         thepsampbytes[1] = bufpsampbyte;
  260.         psampsm[i] = thepsamp;
  261.     }
  262.     writtensofar += numsamples;
  263.     fwrite(psampsm, sizeof(short), numsamples * 2,myfilehandle);
  264.     return true;
  265. }
  266.  
  267. bool rec::Finish() {
  268.     fflush(myfilehandle);
  269.     fclose(myfilehandle);
  270.     return true;
  271. }
  272.  
  273. char * rec::OutputSize() {
  274.     static char megs[32];
  275.     if (bitdepth == 1) {
  276.         sprintf(megs, "%.2fM", ((float)writtensofar * 4.0f / 1024.0f / 1024.0f));
  277.     } else {
  278.         sprintf(megs, "%.2fM", ((float)writtensofar * 2.0f * (float)sizeof(float) / 1024.0f / 1024.0f));
  279.     }
  280.     return megs;
  281. }
  282.  
  283. rec *prec;
  284.  
  285. BOOL APIENTRY ConfigDialog (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  286.     switch(uMsg) {
  287.     case WM_INITDIALOG:
  288.     {
  289. //        if (prec->bitdepth == 1) {
  290. //            CheckDlgButton(hDlg,IDC_RADIO16,1);
  291. //        } else {
  292. //            CheckDlgButton(hDlg,IDC_RADIO32,1);
  293. //        }
  294.         return 1;
  295.     }
  296.     case WM_SHOWWINDOW:
  297.     {
  298.         return 1;
  299.     }
  300.     case WM_CLOSE:
  301.     {
  302.         EndDialog (hDlg, TRUE);
  303.     }
  304.     case WM_COMMAND:
  305.         switch ( LOWORD (wParam))
  306.         {
  307.         case IDOK:
  308.             
  309. //            if (IsDlgButtonChecked(hDlg, IDC_RADIO32) == 1) { prec->bitdepth = 0; }
  310. //            if (IsDlgButtonChecked(hDlg, IDC_RADIO16) == 1) { prec->bitdepth = 1; }
  311.  
  312.             EndDialog (hDlg, TRUE);
  313.             break;
  314.         case IDCANCEL:
  315.             EndDialog (hDlg, TRUE);
  316.             break;
  317.         default:
  318.             return 0;
  319.         }
  320.         break;
  321.     }
  322.     return 0;
  323. }
  324.  
  325. void rec::ConfigDlg (HWND parentwindow) {
  326.     prec = this;
  327.     DialogBox(dllInstance, MAKEINTRESOURCE (IDD_CONFIG), parentwindow, (DLGPROC) &ConfigDialog);
  328. }
  329.  
  330. void rec::DispatchCommand(int command_id, int param1, int param2, int param3, int param4) {
  331.  
  332. }
  333.  
  334. void rec::DispatchCommandEx(char * str_command, char * str_value) { 
  335.     if (strcmp(str_command, "about") == 0) {
  336.         MessageBox(*(HWND*)str_value, "Next\\Sun Audio Recorder 1.2\nCoded by Edward L. Blake aka Cyanphase\nCopyright 2002 Edward L. Blake", "About Next\\Sun Audio Recorder", MB_OK | MB_ICONINFORMATION);
  337.     }
  338. }
  339.  
  340. extern "C" {
  341. __declspec(dllexport) COOERecorder * __cdecl CreateRecorder() { return new rec; }
  342. __declspec(dllexport) char * __cdecl RecInfo() { return "Next\\Sun AU Recorder"; }
  343. }
  344.  
  345. BOOL WINAPI DllMain ( HANDLE hModule, DWORD fwdreason, LPVOID lpReserved )
  346. {
  347.     switch (fwdreason) {
  348.     case DLL_PROCESS_ATTACH: { dllInstance = (HINSTANCE) hModule; } break;
  349.     case DLL_THREAD_ATTACH: break;
  350.     case DLL_THREAD_DETACH: break;
  351.     case DLL_PROCESS_DETACH: break;
  352.     }
  353.     return TRUE;
  354. }
  355.